package vip.xiaonuo.vein.service.sn;

import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RedisSnGenerate implements SnNoGenerateStrategy{

    @Resource
    private RedissonClient client;

    private static final String SN_NO_KEY = "CHAT_NO_";

    @Override
    public String getSnNo(String deviceNo){
        String key = SN_NO_KEY + deviceNo;
        List<Integer> indexList = new ArrayList<>();
        RBucket<List<Integer>> bucket = client.getBucket(key);

        if(!bucket.isExists()){
            for(int i = 0 ; i < CODE_LENGTH ; i++ ){
                indexList.add(0);
            }
            bucket.set(indexList);
        }

        indexList = bucket.get();

        StringBuilder sb = new StringBuilder();
        for(int i = 0  ; i < indexList.size()   ; i++){
            sb.append(str.charAt(indexList.get(i)));
        }

        for(int i = indexList.size()-1  ; i >= 0 ; i--){
            indexList.set(i,indexList.get(i)+1);
            if(indexList.get(i) > str.length()-1){
                indexList.set(i,0);
            }else{
                break;
            }
        }
        bucket.set(indexList);
        return sb.toString();
    }


}
